package com.init.engineering.dao;


import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pojo.Developer;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface DeveloperDao {

    /**
     * 通过did找到唯一的开发商
     * @param did
     * @return
     */
    @Select("select * from developer where did = #{did} and status = 1")
    Developer findDevByDid(String did);

    /**
     * 找到所有的开发商
     * @return
     */
    @Select("select * from developer where status = 1")
    List<Developer> findDevelopers();


    /**
     * 分页查询所有开发商
     * @param queryPageBean
     * @return
     */
    @Select({"<script> SELECT * FROM developer <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and dname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where> limit #{currentPage},#{pageSize}" +
            "</script>"})
    List<Developer> findDevelopersByPage(QueryPageBean queryPageBean);


    /**
     * 符合分页查询的总条数
     * @param queryString
     * @return
     */
    @Select({"<script> SELECT count(*) FROM developer <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and dname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where>" +
            "</script>"})
    long selectTotalCount(@Param("queryString")String queryString);



    /**
     * 逻辑删除，执行更新操作，将属性status的值修改为0，表示将该数据删除
     * @param did
     * @return
     */
    @Update("UPDATE developer set status = 0 where did = #{did}")
    int updateStatusDeveloper(@Param("did")String did);

    /**
     * 新增一个开发商
     * @param developer
     * @return
     */
    @Insert("insert into developer(dname,did,updatetime,status)" +
            " values(#{dname},#{did},#{updatetime},1)")
    int saveDeveloper(Developer developer);

    /**
     * 更新开发商信息:但不能更新status属性
     * @param developer
     * @return
     */
    @Update("<script>update developer <set>" +
            "<if test='dname != null'>dname = #{dname},</if>" +
            "<if test='updatetime != null'>updatetime = #{updatetime}</if>" +
            "</set> where did = #{did}</script>")
    int updateDeveloper(Developer developer);
}
